<template>
    <div id="release">
        <el-row>
            <el-col :span="18"
                    :offset="2">
                <div class="grid-content bg-white">
                    <div class="top_box bg-blue">
                        <div class="title_box">
                            <span class="title_style">Haring knowledge</span>
                        </div>
                        <div class="guide_box">
                            <span class="guide_style">发布公告</span>
                        </div>
                        <div class="image_box">
                            <img src="~@/assets/release/image1.png" />
                        </div>
                    </div>
                    <div class="cell"></div>

                    <div class="operate_box">

                            <div class="title1_box">
              <span class="title1_style">
                公告标题
              </span>
                            </div>
                            <div class="title_input">
                                <el-input v-model="state.form.title"
                                          :rows="1"
                                          type="textarea"
                                          placeholder="请简短的输入任务标题" />
                            </div>
                            <div class="content_box">
              <span class="content_style">
                公告详情
              </span>
                            </div>
                            <div class="content_input">
                                <el-input  type="textarea" :rows="30" size="small" placeholder="请输入公告内容" v-model="state.form.notice_detail"></el-input>
                            </div>
                            <div class="data_box">
              <span class="data_style">
                附件
              </span>
                            </div>
                            <div class="upload_box">
                                <upload-file ref="uploadFile"></upload-file>
                                <el-dialog>
                                    <img src="~@/assets/release/group.png" />
                                </el-dialog>
                            </div>
                            <div class="title1_box">
                              <span class="title1_style">
                                指定部门权限
                              </span>
                            </div>
                            <div class="group_box">
                                <el-checkbox-group v-model="state.checkboxGroup1" size="mini">
                                    <el-checkbox v-for="(inx1,index) in state.bmdata" :key="index" :label="inx1.id" border>{{inx1.label}}</el-checkbox>
                                </el-checkbox-group>
                            </div>
                            <div class="title1_box">
                          <span class="title1_style">
                            单独设定人员【勾选者可见】
                          </span>
                            </div>
                            <div class="radio_box">
                                <el-tree
                                        :data="state.selectData"
                                        show-checkbox
                                        node-key="id"
                                        :default-expanded-keys="state.defaultExpandedKeys"
                                        :default-checked-keys="state.defaultExpandedKeys"
                                        ref="tree"
                                        :collapse-tags="true"
                                >
                                </el-tree>
                            </div>
                        <div class="cell1_box"></div>
                        <div class="submit_box" @click="addRelea">
                            <el-button class="submit_style">发布</el-button>
                        </div>
                    </div>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
    import { getCurrentInstance, ref ,reactive, onMounted} from "vue";
    import { getDepartment, postaddNotice } from "@/request/api";
    import uploadFile from '@/components/uploadFile/uploadFile'
    const state = reactive({
        checkboxGroup1: [],
        bmdata: [],
        defaultExpandedKeys: [],
        defaultCheckedKeys: [],
        selectData: [],
        form: {
            title : '',
            notice_detail:'',
            about_file:''
        }
    })
    export default {
        components: {uploadFile},
        data: () => ({}),
        created () {
            this.editorConfig = {
                UEDITOR_HOME_URL: '/UEditor/',
                // 服务端接口（这个地址是我为了方便各位体验文件上传功能搭建的临时接口，请勿在生产环境使用！！！）
                serverUrl: 'https://inter.huanxingbi.cn/ueditor/php/controller.php',
                toolbars: [[
                    'justifyjustify', 'undo', 'bold', '|', 'simpleupload', 'forecolor', 'justifycenter',
                    'justifyleft', 'horizontal', 'indent', 'fontsize', 'fontfamily'
                ]]
            }
        },
        setup (props,{emit}) {
            const radio2 = ref("");
            const radio3 = ref("");
            const {ctx,proxy} = getCurrentInstance();
            //获取部门
            const init = () => {
                let param = {
                    pageSize: 100
                }
                getDepartment(param).then(res => {
                    state.bmdata = res.data.data
                    state.selectData   = res.data.list
                })
            }
            //发布
            const addRelea = () => {
               

                if(!state.form.title){
                    proxy.$message.error('请输入标题')
                    return false
                }
                if(!state.form.notice_detail){
                    proxy.$message.error('请输入详情')
                    return false
                }
                state.form.about_file = proxy.$refs.uploadFile.$refs.uploadRef.uploadFiles[0].response.data.url
                if(!state.form.about_file){
                    proxy.$message.error('请上传文件')
                    return false
                }
                let rules = proxy.$refs.tree.getCheckedKeys();
                let detree = state.checkboxGroup1;

                let data = '*'
                let bmdata = ''
                if(rules.length > 0) {
                    data = userArr(rules);
                }
                if(detree.length > 0){
                    bmdata = userArr(detree);
                    state.form.department = bmdata
                }
                state.form.user_arr = data;
                postaddNotice(state.form).then(res => {
                    if (res.ret == 200) {
                        proxy.$message.success('发布公告成功！')
                        setTimeout(function () {
                            window.location.href = 'https://nw2.huanxingbi.cn/#/user-List-16'
                        },1000)
                    } else {
                        proxy.$message.error(res.msg)
                    }
                })
            }
            const userArr = (data) => {
                let arr = []
                for (let i = 0;data.length > i;i++){
                    if(data[i] > 0){
                        arr.push(data[i]);
                    }
                }
                return arr.join(',');
            }
            const count = ref(1);
            onMounted(async () => {
                init();
            })
            return {
                title: ref(""),
                content: ref(""),
                radio1: ref('现金'),
                state,
                radio2,
                radio3,
                props,
                count,
                addRelea,
                userArr
            };
        }
    };
</script>

<style lang="scss" scoped>
    .grid-content {
        min-height: 1208px;
        border-radius: 25px;
    }
    .bg-white {
        background-color: #ffffff;
    }
    .top_box {
        height: 220px;
        border-top-left-radius: 25px;
        border-top-right-radius: 25px;
        position: relative;
    }
    .bg-blue {
        background-image: linear-gradient(#ffffff, #d0e3fe);
    }
    .title_box {
        position: absolute;
        top: 22px;
        left: 63px;
        height: 36px;
        .title_style {
            font-size: 34px;
            font-family: Microsoft YaHei;
            font-weight: bold;
            color: #4e72fc;
            line-height: 100px;
        }
    }
    .guide_box {
        position: absolute;
        top: 103px;
        left: 63px;
        height: 17px;
        .guide_style {
            font-size: 18px;
            font-family: Microsoft YaHei;
            color: #7a8499;
            font-weight: 400;
        }
    }
    .image_box {
        position: absolute;
        bottom: 0;
        right: 33px;
    }
    .cell {
        margin-top: 3px;
        height: 5px;
        background-color: #e2e9f7;
    }
    .operate_box {
        min-height: 980px;
        border-bottom-left-radius: 25px;
        border-bottom-right-radius: 25px;
        .title1_box {
            margin-left: 63px;
            margin-top: 30px;
            height: 17px;
            .title_style {
                font-size: 27px;
                font-family: PingFang SC;
                font-weight: bold;
                color: #2b2d33;
                line-height: 100px;
            }
        }
        .title_input {
            margin-left: 63px;
            margin-top: 20px;
            width: 80%;
        }
        .content_box {
            margin-top: 60px;
            margin-left: 63px;
        }
        .content_input {
            margin-top: 20px;
            margin-left: 63px;
            width: 80%;
        }
        .data_box {
            margin-top: 60px;
            margin-left: 63px;
        }
        .upload_box {
            margin-left: 63px;
            margin-top: 10px;
        }
        .group_box {
            margin-top: 30px;
            margin-left: 63px;
            display: flex;
            .group {
                width: 120px;
                height: 52px;
                display: flex;
                background-color: #f0f2f5;
                color: #858688;
                align-items: center;
                border-radius: 26px;
                padding-left: 42px;
                margin-right: 55px;
            }
            .group1 {
                background-color: #f4f1fd;
            }
            .group2 {
                background-color: #f4ebf6;
            }
            .group3 {
                background-color: #eef5fe;
            }
        }
        .type_box {
            margin-top: 30px;
            margin-left: 63px;
            .type_style {
            }
        }
        .type_select {
            margin-top: 20px;
            margin-left: 63px;
            display: flex;
            .demo_button {
                width: 23%;
                .demo_style {
                    height: 45px;
                    background-color: #f0f2f5;
                    color: #858688;
                    border-radius: 10px;
                }
                .demo_style1 {
                    background-color: #e8efff;
                    color: #6192ff;
                }
            }
        }
        .type_input {
            margin-top: 30px;
            margin-left: 60px;
            width: 55%;
        }
        .time_box {
            margin-top: 40px;
            margin-left: 63px;
            .time_style1 {
                color: red;
            }
        }
        .time_radio {
            margin-top: 10px;
            margin-left: 63px;
        }
        .time_input {
            margin-top: 30px;
            margin-left: 63px;
        }
        .department_box {
            width: 80%;
            margin-left: 15%;
            margin-top: 30px;
            margin-bottom: 60px;
            .top {
                display: flex;
                flex-wrap: wrap;
            }
            .bottom {
                margin-top: 20px;
                display: flex;
                flex-wrap: wrap;
            }
            .demo_button {
                width: 25%;
                .demo_style {
                    height: 45px;
                    background-color: #f0f2f5;
                    color: #858688;
                    border-radius: 10px;
                }
                .demo_style1 {
                    background-color: #e8efff;
                    color: #6192ff;
                }
            }
        }
    }

    .cell1_box {
        width: 95%;
        height: 1px;
        background-color: #e3e9f7;
        margin-left: 2.5%;
    }
    .submit_box {
        margin-left: 47%;
        margin-top: 40px;
        padding-bottom: 50px;
        .submit_style {
            width: 80px;
            height: 32px;
            background: #6c91ed;
            box-shadow: 0px 2px 6px 0px rgba(110, 132, 179, 0.2);
            border-radius: 8px;
            color: #ffffff;
        }
    }
    .dfix {
        display: flex;
    }
    .redio-maright {
        display: inline-block;
        margin-right: 30px;

    }
    .el-radio-button__inner {
        background: #d0e3fe !important;
    }
    .tree {
        margin-left: 63px;
        margin-top: 20px;
    }
    .img-se1 {
        padding-top: 25px;
    }
    .radio_box {
        margin-left: 55px;
        margin-top: 20px;
    }
</style>
